﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Entities;

namespace SportsStore.WebUI.Binders
{
    public class CartModelBinder : IModelBinder
    {
        private const string sessionKey = "Cart";

        public object BindModel(ControllerContext ctlContext, ModelBindingContext modelContext) 
        {
            //Get Cart from the session
            Cart c = (Cart)ctlContext.HttpContext.Session[sessionKey];
            //Create if there is no value.
            if (c == null)
            {
                c = new Cart();
                ctlContext.HttpContext.Session[sessionKey] = c;
            }

            return c;
        }
    }
}